// Package sys
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2025 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
// @AutoGenerate Version 2.15.11
package sys

import (
	"context"
	"fmt"
	"hotgo/internal/dao"
	"hotgo/internal/library/hgorm"
	"hotgo/internal/library/hgorm/handler"
	"hotgo/internal/model/input/form"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"
	"hotgo/utility/convert"
	"hotgo/utility/excel"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/encoding/gjson"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/util/gconv"
)

type sSysDistributionDetail struct{}

func NewSysDistributionDetail() *sSysDistributionDetail {
	return &sSysDistributionDetail{}
}

func init() {
	service.RegisterSysDistributionDetail(NewSysDistributionDetail())
}

// Model 分销记录详情ORM模型
func (s *sSysDistributionDetail) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
	return handler.Model(dao.DistributionDetail.Ctx(ctx), option...)
}

// List 获取分销记录详情列表
func (s *sSysDistributionDetail) List(ctx context.Context, in *sysin.DistributionDetailListInp) (list []*sysin.DistributionDetailListModel, totalCount int, err error) {
	mod := s.Model(ctx)

	// 字段过滤
	mod = mod.FieldsPrefix(dao.DistributionDetail.Table(), sysin.DistributionDetailListModel{})
	mod = mod.Fields(hgorm.JoinFields(ctx, sysin.DistributionDetailListModel{}, &dao.AdminMember, "adminMember"))
	mod = mod.Fields(hgorm.JoinFields(ctx, sysin.DistributionDetailListModel{}, &dao.Product, "product"))

	// 关联表字段
	mod = mod.LeftJoinOnFields(dao.AdminMember.Table(), dao.DistributionDetail.Columns().MemberId, "=", dao.AdminMember.Columns().Id)
	mod = mod.LeftJoinOnFields(dao.Product.Table(), dao.DistributionDetail.Columns().ProductId, "=", dao.Product.Columns().Id)

	// 查询id
	if in.Id > 0 {
		mod = mod.Where(dao.DistributionDetail.Columns().Id, in.Id)
	}

	// 查询记录 id
	if in.TraceId != "" {
		mod = mod.WhereLike(dao.DistributionDetail.Columns().TraceId, "%"+in.TraceId+"%")
	}

	// 查询分销时间
	if len(in.CreatedAt) == 2 {
		mod = mod.WhereBetween(dao.DistributionDetail.Columns().CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
	}

	// 查询角色ID
	if in.AdminMemberRoleId > 0 {
		mod = mod.WherePrefix(dao.AdminMember.Table(), dao.AdminMember.Columns().RoleId, in.AdminMemberRoleId)
	}

	// 查询手机号码
	if in.AdminMemberMobile != "" {
		mod = mod.WherePrefixLike(dao.AdminMember.Table(), dao.AdminMember.Columns().Mobile, "%"+in.AdminMemberMobile+"%")
	}

	// 查询微信昵称
	if in.AdminMemberNickname != "" {
		mod = mod.WherePrefixLike(dao.AdminMember.Table(), dao.AdminMember.Columns().Nickname, "%"+in.AdminMemberNickname+"%")
	}

	// 查询类型
	if in.Type > 0 {
		mod = mod.Where(dao.DistributionDetail.Columns().Type, in.Type)
	}

	//

	// 分页
	mod = mod.Page(in.Page, in.PerPage)

	// 排序
	mod = mod.OrderDesc(dao.DistributionDetail.Table() + "." + dao.DistributionDetail.Columns().Id)

	// 查询数据
	if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
		err = gerror.Wrap(err, "获取分销记录详情列表失败，请稍后重试！")
		return
	}
	for i, v := range list {
		list[i].ProductImages = gjson.New(v.ProductMainImage)
	}
	return
}

// Export 导出分销记录详情
func (s *sSysDistributionDetail) Export(ctx context.Context, in *sysin.DistributionDetailListInp) (err error) {
	list, totalCount, err := s.List(ctx, in)
	if err != nil {
		return
	}

	// 字段的排序是依据tags的字段顺序，如果你不想使用默认的排序方式，可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
	tags, err := convert.GetEntityDescTags(sysin.DistributionDetailExportModel{})
	if err != nil {
		return
	}

	var (
		fileName  = "导出分销记录详情-" + gctx.CtxId(ctx)
		sheetName = fmt.Sprintf("索引条件共%v行,共%v页,当前导出是第%v页,本页共%v行", totalCount, form.CalPageCount(totalCount, in.PerPage), in.Page, len(list))
		exports   []sysin.DistributionDetailExportModel
	)

	if err = gconv.Scan(list, &exports); err != nil {
		return
	}

	err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
	return
}

// Edit 修改/新增分销记录详情
func (s *sSysDistributionDetail) Edit(ctx context.Context, in *sysin.DistributionDetailEditInp) (err error) {
	return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {

		// 修改
		if in.Id > 0 {
			if _, err = s.Model(ctx).
				Fields(sysin.DistributionDetailUpdateFields{}).
				WherePri(in.Id).Data(in).Update(); err != nil {
				err = gerror.Wrap(err, "修改分销记录详情失败，请稍后重试！")
			}
			return
		}

		// 新增
		if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
			Fields(sysin.DistributionDetailInsertFields{}).
			Data(in).Insert(); err != nil {
			err = gerror.Wrap(err, "新增分销记录详情失败，请稍后重试！")
		}
		return
	})
}

// Delete 删除分销记录详情
func (s *sSysDistributionDetail) Delete(ctx context.Context, in *sysin.DistributionDetailDeleteInp) (err error) {

	if _, err = s.Model(ctx).WherePri(in.Id).Unscoped().Delete(); err != nil {
		err = gerror.Wrap(err, "删除分销记录详情失败，请稍后重试！")
		return
	}
	return
}

// View 获取分销记录详情指定信息
func (s *sSysDistributionDetail) View(ctx context.Context, in *sysin.DistributionDetailViewInp) (res *sysin.DistributionDetailViewModel, err error) {
	if err = s.Model(ctx).WherePri(in.Id).Scan(&res); err != nil {
		err = gerror.Wrap(err, "获取分销记录详情信息，请稍后重试！")
		return
	}
	return
}
